//
//  PictureView.swift
//  SinaBlog
//
//  Created by 有何不可 on 16/1/7.
//  Copyright © 2016年 有何不可. All rights reserved.
//

import UIKit

let margin:CGFloat = 5
let pictureViewWidth = kScreenWidth - CGFloat(OriginalBlogMargin * 2)
let itemWidth = (pictureViewWidth - 2 * margin) / 3

let ID = "item"

class PictureView: UICollectionView, UICollectionViewDataSource {
    
    var picUrls: [PictureModel]? {
        
        didSet {
            
            let size = getSize()
            
            snp_updateConstraints { (make) -> Void in
                
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            }
            
            reloadData()
        }
    }

    init() {
        super.init(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        
        dataSource = self
        
         backgroundColor = UIColor.whiteColor()
        
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        
        flowLayout.minimumInteritemSpacing = margin
        
        flowLayout.minimumLineSpacing = margin
        
        flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        registerClass(PictureCell.self, forCellWithReuseIdentifier: ID)
    }

    private func getSize() -> CGSize {
        
        let count = picUrls?.count
 
        if count == 1 {
            
            return CGSizeMake(itemWidth, itemWidth)
            
        } else if count == 4 {
            
            return CGSizeMake(2 * itemWidth + margin, 2 * itemWidth + margin)
            
        } else {
            
            let n = (count! - 1) / 3 + 1
            
            return CGSizeMake(3 * itemWidth + margin * 2, itemWidth * CGFloat(n) + margin * CGFloat(n - 1))
        }
        
        return CGSizeZero
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension PictureView {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ID, forIndexPath: indexPath) as! PictureCell
        
        cell.pictureModel = picUrls![indexPath.item]
        
        return cell
    }
    
}


class PictureCell: UICollectionViewCell {
    
    var pictureModel: PictureModel? {
        
        didSet {
            imgView.sd_setImageWithURL(NSURL(string: pictureModel?.thumbnail_pic ?? ""))
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUI() {
        
        addSubview(imgView)
        
        imgView.frame = bounds
    }
    
    private lazy var imgView: UIImageView = UIImageView()
}